package com.iqiyi.pps.epg.api.server.proxy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.pps.epg.api.aop.annotation.SkipAuthCheck;
import com.iqiyi.pps.epg.api.aop.annotation.SkipInterfaceLog;
import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.query.TResponse;
import com.iqiyi.pps.epg.api.model.web.statistic.TStatisticAutoNewData;
import com.iqiyi.pps.epg.api.model.web.statistic.TStatisticAutoNewDataPage;
import com.iqiyi.pps.epg.api.model.web.statistic.TStatisticOcrNewData;
import com.iqiyi.pps.epg.api.model.web.statistic.TStatisticOcrNewDataPage;
import com.iqiyi.pps.epg.api.server.streamOut.AutoExcelFileStream;
import com.iqiyi.pps.epg.api.server.streamOut.OcrExcelFileStream;
import com.iqiyi.pps.epg.api.server.web.statistic.StatisticWebService;
import com.iqiyi.pps.epg.api.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by karkarrotxu on 2017/6/28.
 */
@Service
public class StatisticProxy extends BaseProxy {
    private static Logger logger = LoggerFactory.getLogger(StatisticProxy.class);

    @SkipAuthCheck
    public String postOcrNewData(String data) {
        try {
            StatisticWebService.Iface service = getStatisticService();
            TStatisticOcrNewData ocrNewData = JSONObject.parseObject(data, TStatisticOcrNewData.class);
            Long entityId = service.saveOcrNewData(ocrNewData);
            if (entityId == null || entityId.longValue() == 0) {
                return getResponseWithJsonp(Constants.CODE_FAIL, null, INTERFACE_ERROR);
            } else {
                return getResponseWithJsonp(Constants.CODE_SUC, entityId, DETAIL_RESPONSE_SUC);
            }
        } catch (Exception e) {
            logger.error("[postOcrNewData]", e);
            return getResponseWithJsonp(Constants.CODE_ERROR, null, ERROR_EXIST);
        }
    }

    @SkipAuthCheck
    @SkipInterfaceLog
    public String getOcrNewDataList(Integer pageSize, Integer pageNo, String paramsObj) {
        try {
            StatisticWebService.Iface service = getStatisticService();
            if (pageNo == null) {
                pageNo = 1;
            }
            if (pageSize == null) {
                pageSize = 20;
            }
            Map<String, String> params = new HashMap<>();
            if (paramsObj != null) {
                JSONObject jsonObject = JSONObject.parseObject(paramsObj);
                for (Map.Entry<String, Object> temp : jsonObject.entrySet()) {
                    params.put(temp.getKey(), temp.getValue() + "");
                }
            }
            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);
            TPage page = new TPage(-1, pageNo, pageSize, "addTime", "desc", true);
            TStatisticOcrNewDataPage dataPage = service.getOcrNewData(page, tFilter);
            if (dataPage != null) {
                return Constants.getResponse(Constants.CODE_SUC, dataPage, "操作成功").toJsonString();
            } else {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
            }
        } catch (Exception e) {
            logger.error("[getOcrNewDataList]", e);
            return getResponseWithJsonp(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
    }

    @SkipAuthCheck
    @SkipInterfaceLog
    public String getOcrAvgAndFullPercent(String token, String paramsObj) {
        try {
            StatisticWebService.Iface service = getStatisticService();
            Map<String, String> params = new HashMap<>();
            if (paramsObj != null) {
                JSONObject jsonObject = JSONObject.parseObject(paramsObj);
                for (Map.Entry<String, Object> temp : jsonObject.entrySet()) {
                    params.put(temp.getKey(), temp.getValue() + "");
                }
            }
            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);
            String retValue = service.getOcrAvg(tFilter);
            String retValue2 = service.getOcrFullPercentRate(tFilter);
            JSONObject data = new JSONObject();
            data.put("token", token);
            data.put("avg", retValue);
            data.put("fullPercent", retValue2);
            return Constants.getJsonResponse(Constants.CODE_SUC, data, null);
        } catch (Exception e) {
            logger.error("[getOcrAvgAndFullPercent]", e);
            return getResponseWithJsonp(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
    }

    public Response getOcrExcel(String paramsObj,Integer needPic) {
        try {
            Map<String, String> params = new HashMap<>();
            boolean loadPic = needPic != null && needPic > 0;
            if (paramsObj != null) {
                JSONObject jsonObject = JSONObject.parseObject(paramsObj);
                for (Map.Entry<String, Object> temp : jsonObject.entrySet()) {
                    params.put(temp.getKey(), temp.getValue() + "");
                }
            }
            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);

            String fileName = "export_excel.zip";
            OcrExcelFileStream fileStream = new OcrExcelFileStream(fileName,loadPic);
            fileStream.setFilter(tFilter ) ;
            return Response.ok(fileStream)
                    .header("content-disposition", "attachment;filename=" + fileName)
                    .header("cache-control", "no-cache").build();
        } catch (Exception e) {
            logger.error("[getOcrExcel]", e);
            return Response.serverError().build();
        }
    }

    public Response getAutoExcel(String paramsObj,Integer needPic) {
        try {
            Map<String, String> params = new HashMap<>();
            boolean loadPic = needPic != null && needPic > 0;
            if (paramsObj != null) {
                JSONObject jsonObject = JSONObject.parseObject(paramsObj);
                for (Map.Entry<String, Object> temp : jsonObject.entrySet()) {
                    params.put(temp.getKey(), temp.getValue() + "");
                }
            }
            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);
            String fileName = "export_excel.zip";
            AutoExcelFileStream fileStream = new AutoExcelFileStream(fileName,loadPic);
            fileStream.setFilter(tFilter ) ;
            return Response.ok(fileStream)
                    .header("content-disposition", "attachment;filename=" + fileName)
                    .header("cache-control", "no-cache").build();
        } catch (Exception e) {
            logger.error("[getOcrExcel]", e);
            return Response.serverError().build();
        }
    }

    @SkipAuthCheck
    @SkipInterfaceLog
    public String getAutoDataRate(String token, String paramsObj) {
        try {
            StatisticWebService.Iface service = getStatisticService();
            Map<String, String> params = new HashMap<>();
            if (paramsObj != null) {
                JSONObject jsonObject = JSONObject.parseObject(paramsObj);
                for (Map.Entry<String, Object> temp : jsonObject.entrySet()) {
                    params.put(temp.getKey(), temp.getValue() + "");
                }
            }
            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);
           TResponse retValue = service.getAutoDateRate(tFilter);
            if(Constants.CODE_SUC.equals(retValue.getCode())){
                JSONObject data  = JSON.parseObject(retValue.getJsonData());
                data.put("token", token);
                return Constants.getJsonResponse(Constants.CODE_SUC, data, null);
            }else{
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, null);
            }
        } catch (Exception e) {
            logger.error("[getAutoAvgAndFullPercent]", e);
            return getResponseWithJsonp(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
    }

    @SkipAuthCheck
    @SkipInterfaceLog
    public String getAutoNewDataList(Integer pageSize, Integer pageNo, String paramsObj) {
        try {
            StatisticWebService.Iface service = getStatisticService();
            if (pageNo == null) {
                pageNo = 1;
            }
            if (pageSize == null) {
                pageSize = 20;
            }
            Map<String, String> params = new HashMap<>();
            if (paramsObj != null) {
                JSONObject jsonObject = JSONObject.parseObject(paramsObj);
                for (Map.Entry<String, Object> temp : jsonObject.entrySet()) {
                    params.put(temp.getKey(), temp.getValue() + "");
                }
            }
            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);
            TPage page = new TPage(-1, pageNo, pageSize, "addTime", "desc", true);
            TStatisticAutoNewDataPage dataPage = service.getAutoNewData(page, tFilter);
            if (dataPage != null) {
                return Constants.getResponse(Constants.CODE_SUC, dataPage, "操作成功").toJsonString();
            } else {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
            }
        } catch (Exception e) {
            logger.error("[getAutoNewDataList]", e);
            return getResponseWithJsonp(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
    }

    @SkipAuthCheck
    public String postAutoNewData(String data) {
        try {
            StatisticWebService.Iface service = getStatisticService();
            TStatisticAutoNewData ocrNewData = JSONObject.parseObject(data, TStatisticAutoNewData.class);
            Long entityId = service.saveAutoNewData(ocrNewData);
            if (entityId == null || entityId.longValue() == 0) {
                return getResponseWithJsonp(Constants.CODE_FAIL, null, INTERFACE_ERROR);
            } else {
                return getResponseWithJsonp(Constants.CODE_SUC, entityId, DETAIL_RESPONSE_SUC);
            }
        } catch (Exception e) {
            logger.error("[postAutoNewData]", e);
            return getResponseWithJsonp(Constants.CODE_ERROR, null, ERROR_EXIST);
        }
    }
}




